import Base from "../base.js";
import studentsApi from "../../api/students.js";
import GradlesApi from "../../api/gradles.js";
export default class extends Base {
    data = {
        student: {
            name: "zhangsan",
            age: 21,
            gender: "male",
            address: "四川省成都市",
            gradleId:"",
        },
        gradles: [],
    }
    async add() {
        console.log(this.data.student);
        const data = await studentsApi.add(this.data.student);
    }
    nodesMap() {
        this.saveBtn = $(".save-btn");
        this.gradlesSelectEle = $(".gradles");
    }
    rerender() {
        const { gradles } = this.data;
        const optionStr = _.map(gradles, ({_id,name}) => {
            return `<option value=${_id}>${name}</option>`;
        }).join("");
        const currentGradleId=gradles[gradles.length-1]._id;
        this.data.student.gradleId=currentGradleId;
        this.gradlesSelectEle.html(optionStr).val(currentGradleId);
    }
    handle() {
        const { student } = this.data;
        _.forIn(student, (v, k) => {
            if(k==="gradleId"){
                $(".gradles").on("change",e=>{
                    student.gradleId = e.target.value
                });
                return;
            }
            $(`input[name=${k}]`).on("input", (e) => (student[k] = e.target.value));
        });
        this.saveBtn.on("click", this.add.bind(this));
    }
    // 获取班级列表
    async getGradles() {
        const gradles = await GradlesApi.get();
        this.data.gradles = gradles;
        this.rerender();
    }
    mounted() {
        this.getGradles();
    }
}